package com.ruoyi.engineering.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.attachedData.domain.AttachedData;
import com.ruoyi.attachedData.service.IAttachedDataService;
import com.ruoyi.commission.domain.Commission;
import com.ruoyi.commission.service.ICommissionService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.sample.domain.Sample;
import com.ruoyi.sample.service.ISampleService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.engineering.mapper.EngineeringMapper;
import com.ruoyi.engineering.domain.Engineering;
import com.ruoyi.engineering.service.IEngineeringService;

/**
 * 工程信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-07
 */
@Service
public class EngineeringServiceImpl implements IEngineeringService
{
    @Autowired
    private EngineeringMapper engineeringMapper;

    @Autowired
    private ICommissionService commissionService;

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private IAttachedDataService attachedDataService;

    @Autowired
    private ISampleService sampleService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询工程信息
     *
     * @param id 工程信息主键
     * @return 工程信息
     */
    @Override
    public Engineering selectEngineeringById(Long id)
    {
        return engineeringMapper.selectEngineeringById(id);
    }

    /**
     * 查询工程信息列表
     *
     * @param engineering 工程信息
     * @return 工程信息
     */
    @Override
    public List<Engineering> selectEngineeringList(Engineering engineering)
    {
        //权限 项目经理和实验员 看被分配到的     业主和业主单位看自己单位的
        if(engineering.getIsRoleShow() != null && engineering.getIsRoleShow() == 1){
            Long userId = SecurityUtils.getLoginUser().getUserId();

            JSONObject jb = userService.getEngineeringIdsForUserId(userId);
            Long enterpriseId = (Long) jb.get("enterpriseId");
            List<Long> engineeringIds = (List<Long>) jb.get("engineeringIds");

            if(enterpriseId != 0){
                engineering.setEnterpriseId(enterpriseId);
            }else {
                //项目经理，实验员，劳务实验员
                if (!engineeringIds.isEmpty()) {
                    engineering.setIdsList(engineeringIds);
                } else {
                    List<Long> null_ids = new ArrayList<>();
                    null_ids.add(-1L);
                }
            }
        }

        return engineeringMapper.selectEngineeringList(engineering);
    }

    @Override
    public List<Engineering> selectEngineeringListCount(Engineering engineering)
    {
        return engineeringMapper.selectEngineeringListCount(engineering);
    }
    /**
     * 新增工程信息
     *
     * @param engineering 工程信息
     * @return 结果
     */
    @Override
    public int insertEngineering(Engineering engineering)
    {
        engineering.setCreateTime(DateUtils.getNowDate());
        return engineeringMapper.insertEngineering(engineering);
    }

    /**
     * 修改工程信息
     *
     * @param engineering 工程信息
     * @return 结果
     */
    @Override
    public int updateEngineering(Engineering engineering)
    {
        engineering.setUpdateTime(DateUtils.getNowDate());
        return engineeringMapper.updateEngineering(engineering);
    }

    /**
     * 批量删除工程信息
     *
     * @param ids 需要删除的工程信息主键
     * @return 结果
     */
    @Override
    public int deleteEngineeringByIds(Long[] ids)
    {
        return engineeringMapper.deleteEngineeringByIds(ids);
    }


    /**
     * 批量逻辑删除工程信息
     *
     * @param ids 需要删除的工程信息主键
     * @return 结果
     */
    @Override
    public int logicDeleteEngineering(Long[] ids,String delType,Long delUser)
    {
        return engineeringMapper.logicDeleteEngineering(ids,delType,delUser);
    }

    /**
     * 删除工程信息信息
     *
     * @param id 工程信息主键
     * @return 结果
     */
    @Override
    public int deleteEngineeringById(Long id)
    {
        return engineeringMapper.deleteEngineeringById(id);
    }
}
